// Room 4: Waterfall

#include "defines.txt"
#define temp v255
#define temp2 v254
#define ego_walked_away f248
#define fuse_music_completed f249
#define ego_turned_towards_log f250
#define explosion_completed_log_moved f251
#define explosion_completed_ego_died f252
#define score_music_completed f253
#define log_moved_music_completed f254
#define explosion_sound_completed f255
#define waterfall1_obj o1
#define waterfall2_obj o2
#define waterfall3_obj o3
#define waterfall4_obj o4
#define waterfall5_obj o5
#define waterfall6_obj o6
#define waterfall7_obj o7
#define waterfall8_obj o8
#define waterfall9_obj o9
#define waterfall10_obj o10
#define splash_obj o11
#define wave1_obj o12
#define wave2_obj o13
#define log_obj o14
#define explosion_obj o15

if (new_room) {
  load.pic(room_no);
  draw.pic(room_no);
  discard.pic(room_no);

  load.sound(score_music);
  load.sound(log_moved_music);
  load.sound(explosion_sound);
  load.sound(fuse_music);
  load.sound(death_music);

  load.view(40);
  load.view(41);
  load.view(42);
  load.view(43);
  load.view(4);

  animate.obj(waterfall1_obj);
  set.view(waterfall1_obj, 40);
  position(waterfall1_obj, 91, 96);
  set.priority(waterfall1_obj, 8);
  temp = 1;
  cycle.time(waterfall1_obj, temp);
  ignore.objs(waterfall1_obj);
  draw(waterfall1_obj);

  animate.obj(waterfall2_obj);
  set.view(waterfall2_obj, 40);
  set.loop(waterfall2_obj, 1);
  position(waterfall2_obj, 91, 96);
  set.priority(waterfall2_obj, 8);
  cycle.time(waterfall2_obj, temp);
  ignore.objs(waterfall2_obj);
  draw(waterfall2_obj);

  animate.obj(waterfall3_obj);
  set.view(waterfall3_obj, 40);
  set.loop(waterfall3_obj, 1);
  set.cel(waterfall3_obj, 2);
  position(waterfall3_obj, 102, 96);
  set.priority(waterfall3_obj, 9);
  cycle.time(waterfall3_obj, temp);
  ignore.objs(waterfall3_obj);
  draw(waterfall3_obj);

  animate.obj(waterfall4_obj);
  set.view(waterfall4_obj, 40);
  set.loop(waterfall4_obj, 0);
  set.cel(waterfall4_obj, 1);
  position(waterfall4_obj, 92, 39);
  set.priority(waterfall4_obj, 8);
  cycle.time(waterfall4_obj, temp);
  ignore.objs(waterfall4_obj);
  draw(waterfall4_obj);

  animate.obj(waterfall5_obj);
  set.view(waterfall5_obj, 40);
  set.loop(waterfall5_obj, 1);
  position(waterfall5_obj, 92, 39);
  set.priority(waterfall5_obj, 8);
  cycle.time(waterfall5_obj, temp);
  ignore.objs(waterfall5_obj);
  draw(waterfall5_obj);

  animate.obj(waterfall6_obj);
  set.view(waterfall6_obj, 40);
  set.loop(waterfall6_obj, 1);
  position(waterfall6_obj, 102, 52);
  set.priority(waterfall6_obj, 8);
  cycle.time(waterfall6_obj, temp);
  ignore.objs(waterfall6_obj);
  draw(waterfall6_obj);

  animate.obj(waterfall7_obj);
  set.view(waterfall7_obj, 40);
  set.loop(waterfall7_obj, 2);
  position(waterfall7_obj, 94, 110);
  set.priority(waterfall7_obj, 9);
  cycle.time(waterfall7_obj, temp);
  ignore.objs(waterfall7_obj);
  ignore.blocks(waterfall7_obj);
  object.on.anything(waterfall7_obj);
  draw(waterfall7_obj);

  animate.obj(waterfall8_obj);
  set.view(waterfall8_obj, 40);
  set.loop(waterfall8_obj, 2);
  set.cel(waterfall8_obj, 2);
  position(waterfall8_obj, 95, 71);
  set.priority(waterfall8_obj, 8);
  cycle.time(waterfall8_obj, temp);
  ignore.objs(waterfall8_obj);
  draw(waterfall8_obj);

  animate.obj(waterfall9_obj);
  set.view(waterfall9_obj, 40);
  position(waterfall9_obj, 90, 61);
  set.priority(waterfall9_obj, 8);
  cycle.time(waterfall9_obj, temp);
  ignore.objs(waterfall9_obj);
  draw(waterfall9_obj);

  animate.obj(waterfall10_obj);
  set.view(waterfall10_obj, 40);
  set.cel(waterfall10_obj, 2);
  position(waterfall10_obj, 88, 61);
  set.priority(waterfall10_obj, 9);
  cycle.time(waterfall10_obj, temp);
  ignore.objs(waterfall10_obj);
  draw(waterfall10_obj);

  animate.obj(splash_obj);
  set.view(splash_obj, 41);
  set.cel(splash_obj, 2);
  position(splash_obj, 90, 112);
  set.priority(splash_obj, 10);
  cycle.time(splash_obj, temp);
  ignore.objs(splash_obj);
  draw(splash_obj);

  animate.obj(wave1_obj);
  set.view(wave1_obj, 42);
  position(wave1_obj, 63, 111);
  set.priority(wave1_obj, 4);
  temp = 2;
  cycle.time(wave1_obj, temp);
  ignore.objs(wave1_obj);
  ignore.blocks(wave1_obj);
  draw(wave1_obj);

  animate.obj(wave2_obj);
  set.view(wave2_obj, 42);
  set.loop(wave2_obj, 1);
  position(wave2_obj, 35, 123);
  set.priority(wave2_obj, 4);
  cycle.time(wave2_obj, temp);
  ignore.objs(wave2_obj);
  ignore.blocks(wave2_obj);
  draw(wave2_obj);

  animate.obj(log_obj);
  set.view(log_obj, 43);
  set.loop(log_obj, 0);
  set.cel(log_obj, 0);
  if (obj.in.room(dynamite, room_no)) {
    set.cel(log_obj, 1);
  }
  if (log_moved) {
    set.cel(log_obj, 2);
    temp = waterfall_pri_update;
    load.pic(temp);
    overlay.pic(temp);
    discard.pic(temp);
  }
  if (obj.in.room(rope, room_no)) {
    set.cel(log_obj, 3);
  }
  position(log_obj, 39, 159);
  set.priority(log_obj, 14);
  ignore.objs(log_obj);
  ignore.blocks(log_obj);
  draw(log_obj);
  stop.cycling(log_obj);

  reset(ego_walked_away);
  reset(fuse_music_completed);
  reset(ego_turned_towards_log);
  reset(explosion_completed_log_moved);
  reset(explosion_completed_ego_died);
  reset(score_music_completed);
  reset(log_moved_music_completed);
  reset(explosion_sound_completed);

  load.view(0);
  load.view(1);

  if(posn(ego, 0, 107, 13, 113)) {
    position(ego, 1, 114);
  }

  draw(ego);
  show.pic();
}

if (!anim_begun) {
  if (ego_on_water) {
    set.view(ego, 1);
  }
  else {
    set.view(ego, 0);
  }

  if (posn(ego, 59, 103, 77, 110)) {
    set.view(ego, 1);
  }
  if (posn(ego, 87, 105, 115, 124)) {
    set.view(ego, 1);
  }
  if (posn(ego, 47, 114, 60, 121)) {
    set.view(ego, 1);
  }
  if (posn(ego, 48, 97, 55, 100)) {
    set.view(ego, 1);
  }

  if (posn(ego, 79, 104, 107, 112)) {
    set.priority(ego, 9);
  }
  else {
    release.priority(ego);
  }
}

if (said("look")) {
  print("You can feel the cold mist emanating from the beautiful waterfall which dominates the scene. A wall of large boulders blocks the passage to the east.");
}

if (said("look", "tree")) {
  print("You see several trees, the largest of which is leaning. It must be hundreds of years old. One of the trees appears to have a hole in the trunk.");
}

if (said("look", "stream")) {
  print("The stream flows westward from the waterfall.");
}

if (said("look", "beach")) {
  print("You see a beautiful beach in the distance.");
}

if (said("look", "water")) {
  print("The water in the waterfall and the stream is clean but cold.");
}

if (said("look", "water", "pond")) {
  print("There is nothing of interest in the water in the pond.");
}

if (said("look", "waterfall")) {
  print("The water cascades down into the pond from high above.");
}

if (said("look", "behind", "waterfall")) {
  print("There is nothing behind the waterfall, save for a rock wall.");
}

if (said("look", "pond")) {
  print("There is a shallow pond at the base of the waterfall.");
}

if (said("look", "rocks")) {
  print("The rocks in this area are covered in moss.");
}

if (said("look", "moss")) {
  print("The moss on the rocks must be due to the mist from the waterfall.");
}

if (said("take", "moss")) {
  print("You don't need the moss");
}

if (said("climb", "rocks")) {
  print("The rocks are too slippery for you to climb.");
}

if ((said("look", "hole") || said("look", "hole", "rol"))) {
  if (posn(ego, 25, 96, 40, 100)) {
    if (obj.in.room(magnifying_glass, room_no)) {
      print("You peek into the hole and see a magnifying glass hidden within.");
      set(magnifying_glass_found);
    } else {
      print("There is nothing in the hole.");
    }
  } else {
    if (!log_moved) {
      print("There is a hole in the tree, but you can't see the inside of the hole from here.");
    } else {
      if (posn(ego, 34, 146, 65, 160)) {
        print("The hole is completely dark. You can't see into the hole. It seems to be quite deep, though.");
      } else {
        print("There are two holes: the hole in the tree where you got the magnifying glass and the hole in the ground. Get closer to either one and try looking again.");
      }
    }
  }
}

if (said("get", "magnifying glass")) {
  if (magnifying_glass_found) {
    if (!has(magnifying_glass)) {
      if (posn(ego, 25, 96, 40, 100)) {
        print("You take the magnifying glass into possession.");
        get(magnifying_glass);
        score += 2;
        sound(score_music, score_music_completed);
      } else {
        print("You're unable to reach into the hole from this distance.");
      }
    } else {
      print("You already have it.");
    }
  } else {
    reset(input_parsed);
  }
}

if (magnifying_glass_found) {
  if (said("look", "magnifying glass")) {
    if (has(magnifying_glass)) {
      reset(input_parsed);
    } else {
      print("You need to pick it up first.");
    }
  }
}

if (log_moved) {
  if ((said("tie", "rope") || said("attach", "rope"))) {
    print("Please be more specific. What do you want to tie the rope to?");
  }
  if ((said("tie", "rope", "log") || said("attach", "rope", "log"))) {
    if (has(rope)) {
      if (posn(ego, 31, 144, 60, 160)) {
        print("You tie the rope around the log.");
        put(rope, room_no);
        set.cel(log_obj, 3);
        score += 2;
        sound(score_music, score_music_completed);
      } else {
        print("You should be in front of the log to do that.");
      }
    } else {
      print("You already did that.");
    }
  }
  if (obj.in.room(rope, room_no)) {
    if ((said("look", "rope", "rol") || said("look", "log", "rol"))) {
      print("You have attached the rope around the log.");
    }
    if ((said("get", "rope", "rol") || said("remove", "rope", "rol") || said("untie", "rope", "rol"))) {
      if (posn(ego, 31, 144, 60, 160)) {
        print("You untie the rope and remove it from around the log.");
        get(rope);
        set.cel(log_obj, 2);
        score -= 2;
      } else {
        print("You should be in front of the log to do that.");
      }
    }
    if ((said("climb", "hole") ||
         said("climb", "down") ||
         said("climb", "down", "rope", "rol") ||
         said("climb", "down", "hole", "rope", "rol") ||
         said("climb", "hole", "rope", "rol") ||
         said("climb", "down", "hole") ||
         said("jump", "hole") ||
         said("jump", "down", "hole") ||
         said("enter", "hole") ||
         said("climb", "hole", "rope", "rol") ||
         said("climb", "rope", "rol") ||
         said("use", "rope", "hole", "rol") ||
         said("jump", "hole", "rope", "rol") ||
         said("use", "rope", "jump", "hole", "rol") ||
         said("use", "rope", "climb", "hole", "rol") ||
         said("use", "rope", "climb", "down", "hole", "rol") ||
         said("use", "rope", "enter", "hole", "rol") ||
         said("use", "rope", "descend", "hole", "rol") ||
         said("use", "rope", "descend", "down", "hole", "rol") ||
         said("enter", "hole", "rope", "rol") ||
         said("descend", "hole", "rope", "rol") ||
         said("descend", "down", "hole", "rope", "rol"))) {
      if (posn(ego, 31, 144, 60, 160)) {
        score += 3;
        set.cel(log_obj, 4);
        load.view(45);
        set.view(ego, 45);
        set.loop(ego, 0);
        set.cel(ego, 0);
        stop.motion(ego);
        ignore.objs(ego);
        ignore.blocks(ego);
        reposition.to(ego, 50, 158);
        program.control();
        prevent.input();
        set(anim_begun);
        end.of.loop(ego, game_completed);
      } else {
        print("You're not close enough.");
      }
    }
  } else {
    if ((said("climb", "hole") ||
         said("climb", "down") ||
         said("climb", "down", "hole") ||
         said("jump", "hole") ||
         said("jump", "down", "hole") ||
         said("enter", "hole"))) {
       print("You don't want to just jump down there. You need to come up with a method of entering the hole in a relatively safe manner.");
    }
    if ((said("climb", "rope", "rol") ||
         said("climb", "down", "rope", "rol") ||
         said("climb", "down", "hole", "rope", "rol") ||
         said("climb", "hole", "rope", "rol") ||
         said("climb", "hole", "rope", "rol") ||
         said("use", "rope", "hole", "rol") ||
         said("jump", "hole", "rope", "rol") ||
         said("use", "rope", "jump", "hole", "rol") ||
         said("use", "rope", "climb", "hole", "rol") ||
         said("use", "rope", "climb", "down", "hole", "rol") ||
         said("use", "rope", "enter", "hole", "rol") ||
         said("use", "rope", "descend", "hole", "rol") ||
         said("use", "rope", "descend", "down", "hole", "rol") ||
         said("enter", "hole", "rope", "rol") ||
         said("descend", "hole", "rope", "rol") ||
         said("descend", "down", "hole", "rope", "rol"))) {
      print("Attach the rope to something first.");
    }
  }
  if (said("look", "log")) {
    print("Despite the explosion, the log has remained mostly intact.");
  }
  if ((said("climb", "hole") ||
       said("climb", "down") ||
       said("climb", "down", "hole") ||
       said("jump", "hole") ||
       said("jump", "down", "hole") ||
       said("enter", "hole"))) {
    print("You can't see the bottom of the hole. It could be dangerous to simply jump down there. You should find a way to safely descend into the hole.");
  }
} else {
  if (said("look", "log")) {
    print("There is a small but heavy-looking log on the south bank of the stream.");
  }
  if (said("look", "under", "log")) {
    if (posn(ego, 32, 150, 65, 162)) {
      print("Despite your best efforts, you can't see under the log. There seems to be some sort of a depression or a hole below the log, though.");
    } else {
      print("You're not close enough.");
    }
  }
  if ((said("move", "log") ||
       said("push", "log") ||
       said("pull", "log"))) {
    if (posn(ego, 32, 150, 65, 162)) {
      print("The log is too heavy for you to move with your bare hands.");
    } else {
      print("You're not close enough.");
    }
  }
  if ((said("move", "log", "rope") ||
       said("tie", "rope", "log") ||
       said("push", "log", "rope") ||
       said("pull", "log", "rope") ||
       said("use", "rope", "log", "rol") ||
       said("use", "rope", "move", "log", "rol") ||
       said("use", "rope", "pull", "log", "rol"))) {
    print("Moving the log with the rope is a good idea, but the log is too heavy to be moved in that manner.");
  }
  if ((said("tie", "rope", "log") || said("tie", "log", "rope"))) {
    print("Why would you want to do that?");
  }
  if ((said("put", "dynamite", "log", "rol") ||
       said("put", "log", "dynamite", "rol") ||
       said("put", "dynamite", "under", "log", "rol"))) {
    if (has(dynamite)) {
      if (posn(ego, 34, 157, 63, 162)) {
        print("You place the stick of dynamite under the log.");
        put(dynamite, room_no);
        set.cel(log_obj, 1);
        score += 1;
        sound(score_music, score_music_completed);
      } else {
        print("You should be in front of the log to do that.");
      }
    } else {
      if (obj.in.room(dynamite, room_no)) {
        print("You already did that.");
      } else {
        print("You don't have any dynamite.");
      }
    }
  }
  if (obj.in.room(dynamite, room_no)) {
    if ((said("get", "dynamite", "rol") || said("remove", "dynamite", "rol"))) {
      if (posn(ego, 34, 157, 63, 162)) {
        print("You retrieve the stick of dynamite.");
        get(dynamite);
        set.cel(log_obj, 0);
        score -= 1;
      } else {
        print("You're not close enough.");
      }
    }
    if ((said("light", "dynamite") || said("light", "fuse"))) {
      print("Please be more specific. How do you intend to do that?");
    }
    if ((said("light", "dynamite", "magnifying glass") ||
         said("light", "fuse", "magnifying glass") ||
         said("light", "dynamite", "fuse", "magnifying glass") ||
         said("light", "fuse", "dynamite", "magnifying glass") ||
         said("use", "magnifying glass", "fuse") ||
         said("use", "magnifying glass", "dynamite") ||
         said("use", "magnifying glass", "dynamite", "fuse") ||
         said("use", "magnifying glass", "fuse", "dynamite") ||
         said("use", "magnifying glass", "light", "dynamite") ||
         said("use", "magnifying glass", "light", "fuse") ||
         said("use", "magnifying glass", "light", "dynamite", "fuse") ||
         said("use", "magnifying glass", "light", "fuse", "dynamite"))) {
      if (!has(magnifying_glass)) {
        print("You don't have it.");
      } else {
        if (posn(ego, 34, 157, 63, 162)) {
          print("You use the magnifying glass to concentrate the sun's light on the fuse on the stick of dynamite. After a few tries, you manage to ignite the fuse.");
          print("Time to leave!");
          prevent.input();
          program.control();
          set.loop(log_obj, 1);
          set.cel(log_obj, 0);
          start.cycling(log_obj);
          move.obj(ego, 85, 165, 0, ego_walked_away);
          sound(fuse_music, fuse_music_completed);
        } else {
          print("You're not close enough.");
        }
      }
    }
  }
  if ((said("light", "dynamite", "magnifying glass") ||
       said("light", "fuse", "magnifying glass") ||
       said("light", "dynamite", "fuse", "magnifying glass") ||
       said("light", "fuse", "dynamite", "magnifying glass") ||
       said("use", "magnifying glass", "fuse") ||
       said("use", "magnifying glass", "dynamite") ||
       said("use", "magnifying glass", "dynamite", "fuse") ||
       said("use", "magnifying glass", "fuse", "dynamite") ||
       said("use", "magnifying glass", "light", "dynamite") ||
       said("use", "magnifying glass", "light", "fuse") ||
       said("use", "magnifying glass", "light", "dynamite", "fuse") ||
       said("use", "magnifying glass", "light", "fuse", "dynamite"))) {
    if ((!has(dynamite) || !has(magnifying_glass))) {
      print("You don't have it.");
    } else {
      if (posn(ego, 34, 157, 63, 162)) {
        print("Holding the stick of dynamite in your hand, you use the magnifying glass to concentrate the sun's light on the fuse. After a few tries, you manage to ignite the fuse.");
        load.view(44);
        get.posn(ego, temp, temp2);
        if (temp2 < 160) {
          temp2 = 160;
          reposition.to.v(ego, temp, temp2);
        }
        temp = 254;
        temp2 = 3;
        reposition(ego, temp, temp2);
        set.view(ego, 4);
        set.loop(ego, 0);
        set.cel(ego, 0);
        stop.motion(ego);
        start.cycling(ego);
        ignore.blocks(ego);
        ignore.objs(ego);
        program.control();
        prevent.input();
        set(anim_begun);
        end.of.loop(ego, death_finished);
        animate.obj(explosion_obj);
        set.view(explosion_obj, 44);
        set.loop(explosion_obj, 0);
        set.cel(explosion_obj, 0);
        get.posn(ego, temp, temp2);
        temp -= 15;
        temp2 -= 12;
        position.v(explosion_obj, temp, temp2);
        set.priority(explosion_obj, 15);
        ignore.objs(explosion_obj);
        ignore.blocks(explosion_obj);
        draw(explosion_obj);
        end.of.loop(explosion_obj, explosion_completed_ego_died);
        force.update(ego);
        force.update(explosion_obj);
        sound(explosion_sound, explosion_sound_completed);
        shake.screen(1);
      } else {
        reset(input_parsed);
      }
    }
  }
}

if (death_finished) {
  reset(death_finished);
  accept.input();
  death_type = just_died;
}

if (ego_walked_away) {
  reset(ego_walked_away);
  move.obj(ego, 84, 165, 0, ego_turned_towards_log);
}

if (fuse_music_completed && ego_turned_towards_log) {
  reset(fuse_music_completed);
  load.view(44);
  animate.obj(explosion_obj);
  set.view(explosion_obj, 44);
  set.loop(explosion_obj, 0);
  set.cel(explosion_obj, 0);
  position(explosion_obj, 39, 159);
  set.priority(explosion_obj,14);
  ignore.objs(explosion_obj);
  ignore.blocks(explosion_obj);
  draw(explosion_obj);
  end.of.loop(explosion_obj, explosion_completed_log_moved);
  set.loop(log_obj, 0);
  set.cel(log_obj, 2);
  stop.cycling(log_obj);
  sound(explosion_sound, explosion_sound_completed);
  shake.screen(1);
}

if (explosion_completed_log_moved) {
  reset(explosion_completed_log_moved);
  accept.input();
  player.control();
  erase(explosion_obj);
  discard.view(44);
  temp = waterfall_pri_update;
  load.pic(temp);
  overlay.pic(temp);
  discard.pic(temp);
  show.pic();
  print("The explosion has moved the log a bit, revealing a hole underneath. Good job!");
  set(log_moved);
  score += 3;
  sound(log_moved_music, log_moved_music_completed);
}

if (explosion_completed_ego_died) {
  reset(explosion_completed_ego_died);
  erase(explosion_obj);
  discard.view(44);
}

if (game_completed) {
  new.room(ending);
}

if (ego_edge_code == left_edge) {
  new.room(stream);
}

return();